<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>插入区间</title>
</head>
<body>
  <script>
    function fn(intervals, newInterval) {
      let res = [], len = intervals.length, i = 0, newStart = newInterval[0], newEnd = newInterval[1]
      while (i < len && newStart > intervals[i][1]) {
        // 区键没有重合
        res.push(intervals[i])
        i++
      }
      while (i < len && newEnd >= intervals[i][0]) {
        newStart = Math.min(newStart, intervals[i][0])
        newEnd = Math.max(newEnd, intervals[i][1])
        i++
      }
      res.push([newStart, newEnd])
      while (i < len) {
        res.push(intervals[i])
        i++
      }
      return res
    }
    console.log(fn([[1, 3], [6, 9]], [2, 5]))
  </script>
</body>
</html>